package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.NoteTag;
import cn.com.connext.scrm.service.NoteTagService;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.dto.NoteTagDTO;
import cn.com.connext.scrm.web.rest.vm.NoteTagVM;
import cn.com.connext.scrm.web.rest.vm.PageQueryVM;
import cn.com.connext.scrm.web.rest.vm.QueryIdVM;
import com.codahale.metrics.annotation.Timed;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: organization-service
 * @author: shawn
 * @create: 2019-06-06 13:53
 **/
@RestController
@RequestMapping("/api/noteTag/")
public class NoteTagResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(NoteTagResource.class);

    public NoteTagResource() {
        super("noteTag");
    }

    @Autowired
    private NoteTagService noteTagService;

    @PostMapping(value = "edit")
    @Timed
    @ApiOperation(value = "标签编辑", tags = {"标签管理"}, notes = "标签编辑")
    public ResponseEntity edit(@RequestBody NoteTagVM vm){
        JsonResult result = new JsonResult();
        try{
            NoteTagDTO noteTagDTO = noteTagService.edit(vm);
            result.setState(0);
            result.setMessage("编辑成功");
            return entityResponse(result);
        }catch (Exception e){
            log.error("编辑失败:{}", e);
            result.setMessage("编辑失败");
            result.setState(1);
            return entityResponse(result);
        }
    }

    @PostMapping("delete")
    @Timed
    @ApiOperation(value = "标签编辑", tags = {"标签管理"}, notes = "标签编辑")
    public ResponseEntity delete(@RequestBody QueryIdVM vm){
        JsonResult result = new JsonResult();
        try{
            if (vm == null){
                result.setState(1);
                result.setMessage("参数缺失");
                return entityResponse(result);
            }
            noteTagService.deleteById(vm.getId());
            result.setMessage("删除成功");
            result.setState(0);
            return entityResponse(result);
        }catch (Exception e){
            log.error("删除错误:{}", e);
            result.setState(1);
            result.setMessage("删除错误");
            return entityResponse(result);
        }
    }

    @PostMapping("enable")
    @Timed
    @ApiOperation(value = "标签启用/停用", tags = {"标签管理"}, notes = "标签启用/停用")
    public ResponseEntity enable(@RequestBody QueryIdVM vm){
        JsonResult result = new JsonResult();
        try{
            if (vm == null){
                result.setState(1);
                result.setMessage("参数缺失");
                return entityResponse(result);
            }
            noteTagService.enableById(vm);
            result.setMessage("操作成功");
            result.setState(0);
            return entityResponse(result);
        }catch (Exception e){
            log.error("启用错误:{}", e);
            result.setState(1);
            result.setMessage("启用错误");
            return entityResponse(result);
        }
    }

    @PostMapping("list")
    @Timed
    @ApiOperation(value = "标签列表", tags = {"标签管理"}, notes = "标签列表")
    public ResponseEntity list(@RequestBody PageQueryVM vm){
        JsonResult result = new JsonResult();
        try{
            Page<NoteTag> page = noteTagService.pageQuery(vm);
            return pagedResponse(page);
        }catch (Exception e){
            log.error("查询失败:{}", e);
            result.setState(1);
            result.setMessage("查询失败");
            return entityResponse(result);
        }
    }

    @PostMapping("detail")
    @Timed
    @ApiOperation(value = "标签详情", tags = {"标签管理"}, notes = "标签详情")
    public ResponseEntity detail(@RequestBody QueryIdVM vm){
        JsonResult result = new JsonResult();
        try{
            if (vm == null){
                result.setState(1);
                result.setMessage("参数缺失");
                return entityResponse(result);
            }
            NoteTagDTO dto = noteTagService.findById(vm);
            result.setMessage("查询成功");
            result.setState(0);
            result.setData(dto);
            return entityResponse(result);
        }catch (Exception e){
            log.error("查询失败:{}", e);
            result.setState(1);
            result.setMessage("查询失败");
            return entityResponse(result);
        }
    }

    @PostMapping("categoryList")
    @Timed
    @ApiOperation(value = "标签分类列表", tags = {"标签管理"}, notes = "标签分类列表")
    public ResponseEntity categoryList(){
        JsonResult result = new JsonResult();
        try{
            List<NoteTagDTO> list = noteTagService.findCategoryList();
            result.setMessage("查询成功");
            result.setState(0);
            result.setData(list);
            return entityResponse(result);
        }catch (Exception e){
            log.error("查询失败:{}", e);
            result.setState(1);
            result.setMessage("查询失败");
            return entityResponse(result);
        }
    }

}
